﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using PC.PickleJar.DebugInstaller;
using PC.PickleJar.Domain;
using PC.PickleJar.Exceptions;
using PC.PickleJar.Helpers;
using PC.PickleJar.MSTest.Helpers;
using System;
using System.Reflection;

namespace PC.PickleJar
{
	[Serializable]
	[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
	public sealed class PickleJarTestAttribute : TestClassExtensionAttribute
	{
		private readonly static Uri Id = new Uri("urn:PerfectlyCromulent.PickleJar.PickleJarTest");

		public PickleJarTestAttribute()
		{
			// TODO: remove everything in this pre-processor block.  It will all be handled by a dedicated installer.
#if DEBUG
			var location = Assembly.GetExecutingAssembly().Location;
			if (!location.ToLower().Substring(3).StartsWith("program files"))
			{
				try
				{
					var config = new MSTestConfigurationHelper();
					var registrationHelper = new MSTestRegistrationHelper();
					registrationHelper.CopyFiles(typeof(SourceItemBase), config.VisualStudioVersion);
					registrationHelper.CopyAndRegister(typeof(PickleJarTestAttribute), config.VisualStudioVersion);
				}
				catch (ConfigurationNotFoundException)
				{
					// do nothing.
				}
			}
#endif
		}

		/// <summary>
		/// Set to the Assembly Qualified Type  Name of your custome ITestMethodInvoker.
		/// </summary>
		/// <remarks>
		/// Leave null to use default ITestMethodInvoker.
		/// </remarks>
		public String CustomTestMethodInvokerTypeName
		{
			get;
			set;
		}

		public override Uri ExtensionId
		{
			get
			{
				return PickleJarTestAttribute.Id;
			}
		}

		public override TestExtensionExecution GetExecution()
		{
			return new PickleJarTestExtensionExecution();
		}
	}
}
